package jbase.project.system.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

import jbase.project.system.exception.SysException;

public class ZipUtil {

	public final static void writeZipFile(File zip, Map<String, byte[]> entryData) throws SysException {
		try {
			writeZip(new ZipOutputStream(new FileOutputStream(zip)), entryData);
		} catch (Exception e) {
			throw new SysException(e);
		}
	}

	public final static void writeZip(OutputStream out, Map<String, byte[]> entryData) throws SysException {
		try {
			writeZip(new ZipOutputStream(out), entryData);
		} catch (Exception e) {
			throw new SysException(e);
		} finally {
			IOUtil.close(out);
		}
	}

	public final static void writeZip(ZipOutputStream zos, Map<String, byte[]> entryData) throws SysException {
		try {
			Set<String> entryNames = entryData.keySet();
			Iterator<String> it = entryNames.iterator();
			while (it.hasNext()) {
				String name = it.next();
				byte[] data = entryData.get(name);
				zos.putNextEntry(new ZipEntry(name));
				zos.write(data);
				zos.flush();
				zos.closeEntry();
			}
			zos.close();
		} catch (Exception e) {
			throw new SysException(e);
		} finally {
			IOUtil.close(zos);
		}
	}

	public final static Map<String, byte[]> readZip(InputStream zis) throws SysException {
		try {
			return readZip(new ZipInputStream(zis));
		} catch (SysException e) {
			throw e;
		} finally {
			IOUtil.close(zis);
		}
	}

	public final static Map<String, byte[]> readZip(ZipInputStream zis) throws SysException {
		Map<String, byte[]> data = new HashMap<>();
		try {
			ZipEntry next = zis.getNextEntry();
			while (next != null) {
				int rdCnt = 0;
				String name = next.getName();
				ByteArrayOutputStream out = new ByteArrayOutputStream();
				byte[] buff = new byte[1024];

				rdCnt = zis.read(buff);
				while (rdCnt != -1) {
					out.write(buff, 0, rdCnt);
					out.flush();
					rdCnt = zis.read(buff);
				}
				data.put(name, out.toByteArray());
				out.close();
				next = zis.getNextEntry();
			}
		} catch (IOException e) {
			throw new SysException(e);
		} finally {
			IOUtil.close(zis);
		}
		return data;
	}

	public static void main(String[] args) throws FileNotFoundException, SysException {
		Map<String, byte[]> data = new HashMap();
		data.put("a/bb.txt", new String("a").getBytes());
		data.put("b.doc", new String("你好我").getBytes());
		writeZipFile(new File("d:/doc/aaa.zip"), data);

		Map<String, byte[]> dat = readZip(new FileInputStream(new File("d:/doc/aaa.zip")));
		Set<String> ns = dat.keySet();
		for (String n : ns) {
			System.out.printf("%s:\n%s", n, new String(dat.get(n)));
		}
	}

}
